home *** CD-ROM | disk | FTP | other *** search
/ Programming Microsoft Visual Basic .NET / Programming Microsoft Visual Basic .NET (Microsoft Press)(X08-78517)(2002).bin / setup / vbnet / 17 controls / customcontroldemo / addresscontrol.vb < prev    next >
Encoding:
Text File  |  2002-03-20  |  14.2 KB  |  416 lines

  1. Imports System.ComponentModel
  2.  
  3. ' IMPORTANT NOTE: if you use the LicWindowsFileLicenseProvider license scheme you
  4. '                 must copy the CustomControlsDemo.AddressControl.lic file from the
  5. '                 \BIN subdirectory into the Windows System directory, otherwise
  6. '                 you won't be able to use the AddressControl control at design time.
  7.  
  8. ' change this constant to enforce different types of licensing.
  9. ' can be 0 (no license) 1 (standard file license), 2 (custom file license)
  10.  
  11. ' If you opt for custom file license, ensure that you copy the 
  12. ' CustomControlDemo.AddressControl.lic to the c:\Windows\System directory
  13.  
  14. #Const USELICENSE = 0
  15.  
  16. #If USELICENSE = 1 Then
  17.     <LicenseProvider(GetType(LicFileLicenseProvider))> _
  18.     Public Class AddressControl
  19.  
  20. #ElseIf USELICENSE = 2 Then
  21.     <LicenseProvider(GetType(LicWindowsFileLicenseProvider))> _
  22.     Public Class AddressControl
  23. #Else
  24.     ' no licensing
  25.     Public Class AddressControl
  26. #End If
  27.  
  28.     Inherits System.Windows.Forms.UserControl
  29.  
  30. #Region " Windows Form Designer generated code "
  31.  
  32.     ' The License object
  33.     Private lic As License
  34.  
  35.     Public Sub New()
  36.         MyBase.New()
  37.  
  38. #If USELICENSE > 0 Then
  39.         ' Validate the License.
  40.         lic = LicenseManager.Validate(GetType(AddressControl), Me)
  41. #End If
  42.  
  43.         'This call is required by the Windows Form Designer.
  44.         InitializeComponent()
  45.  
  46.         'Add any initialization after the InitializeComponent() call
  47.     End Sub
  48.  
  49.     'UserControl overrides dispose to clean up the component list.
  50.     Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
  51.         If disposing Then
  52.             ' destroy the license
  53.             If Not (lic Is Nothing) Then
  54.                 lic.Dispose()
  55.                 lic = Nothing
  56.             End If
  57.         End If
  58.         MyBase.Dispose(disposing)
  59.     End Sub
  60.  
  61.     Friend WithEvents Label3 As System.Windows.Forms.Label
  62.     Friend WithEvents Label1 As System.Windows.Forms.Label
  63.     Friend WithEvents Label2 As System.Windows.Forms.Label
  64.     Friend WithEvents Label4 As System.Windows.Forms.Label
  65.     Friend WithEvents Label5 As System.Windows.Forms.Label
  66.     Friend WithEvents txtStreet As System.Windows.Forms.TextBox
  67.     Friend WithEvents txtState As System.Windows.Forms.TextBox
  68.     Friend WithEvents txtCountry As System.Windows.Forms.TextBox
  69.     Friend WithEvents txtCity As System.Windows.Forms.TextBox
  70.     Friend WithEvents txtZip As System.Windows.Forms.TextBox
  71.  
  72.     'Required by the Windows Form Designer
  73.     Private components As System.ComponentModel.Container
  74.  
  75.     'NOTE: The following procedure is required by the Windows Form Designer
  76.     'It can be modified using the Windows Form Designer.  
  77.     'Do not modify it using the code editor.
  78.     <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
  79.         Me.txtStreet = New System.Windows.Forms.TextBox()
  80.         Me.Label4 = New System.Windows.Forms.Label()
  81.         Me.Label5 = New System.Windows.Forms.Label()
  82.         Me.txtState = New System.Windows.Forms.TextBox()
  83.         Me.txtCountry = New System.Windows.Forms.TextBox()
  84.         Me.Label1 = New System.Windows.Forms.Label()
  85.         Me.Label2 = New System.Windows.Forms.Label()
  86.         Me.Label3 = New System.Windows.Forms.Label()
  87.         Me.txtCity = New System.Windows.Forms.TextBox()
  88.         Me.txtZip = New System.Windows.Forms.TextBox()
  89.         Me.SuspendLayout()
  90.         '
  91.         'txtStreet
  92.         '
  93.         Me.txtStreet.Anchor = ((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) _
  94.                     Or System.Windows.Forms.AnchorStyles.Right)
  95.         Me.txtStreet.Location = New System.Drawing.Point(16, 40)
  96.         Me.txtStreet.Name = "txtStreet"
  97.         Me.txtStreet.Size = New System.Drawing.Size(360, 24)
  98.         Me.txtStreet.TabIndex = 1
  99.         Me.txtStreet.Text = ""
  100.         '
  101.         'Label4
  102.         '
  103.         Me.Label4.Location = New System.Drawing.Point(144, 136)
  104.         Me.Label4.Name = "Label4"
  105.         Me.Label4.Size = New System.Drawing.Size(48, 16)
  106.         Me.Label4.TabIndex = 0
  107.         Me.Label4.Text = "&State"
  108.         '
  109.         'Label5
  110.         '
  111.         Me.Label5.Location = New System.Drawing.Point(216, 136)
  112.         Me.Label5.Name = "Label5"
  113.         Me.Label5.Size = New System.Drawing.Size(72, 16)
  114.         Me.Label5.TabIndex = 0
  115.         Me.Label5.Text = "C&ountry"
  116.         '
  117.         'txtState
  118.         '
  119.         Me.txtState.Location = New System.Drawing.Point(144, 160)
  120.         Me.txtState.Name = "txtState"
  121.         Me.txtState.Size = New System.Drawing.Size(56, 24)
  122.         Me.txtState.TabIndex = 1
  123.         Me.txtState.Text = ""
  124.         '
  125.         'txtCountry
  126.         '
  127.         Me.txtCountry.Anchor = ((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) _
  128.                     Or System.Windows.Forms.AnchorStyles.Right)
  129.         Me.txtCountry.Location = New System.Drawing.Point(216, 160)
  130.         Me.txtCountry.Name = "txtCountry"
  131.         Me.txtCountry.Size = New System.Drawing.Size(160, 24)
  132.         Me.txtCountry.TabIndex = 1
  133.         Me.txtCountry.Text = ""
  134.         '
  135.         'Label1
  136.         '
  137.         Me.Label1.Location = New System.Drawing.Point(16, 72)
  138.         Me.Label1.Name = "Label1"
  139.         Me.Label1.Size = New System.Drawing.Size(144, 16)
  140.         Me.Label1.TabIndex = 0
  141.         Me.Label1.Text = "&City"
  142.         '
  143.         'Label2
  144.         '
  145.         Me.Label2.Location = New System.Drawing.Point(16, 136)
  146.         Me.Label2.Name = "Label2"
  147.         Me.Label2.Size = New System.Drawing.Size(72, 16)
  148.         Me.Label2.TabIndex = 0
  149.         Me.Label2.Text = "&Zip"
  150.         '
  151.         'Label3
  152.         '
  153.         Me.Label3.Location = New System.Drawing.Point(16, 16)
  154.         Me.Label3.Name = "Label3"
  155.         Me.Label3.Size = New System.Drawing.Size(144, 16)
  156.         Me.Label3.TabIndex = 0
  157.         Me.Label3.Text = "&Street"
  158.         '
  159.         'txtCity
  160.         '
  161.         Me.txtCity.Anchor = ((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) _
  162.                     Or System.Windows.Forms.AnchorStyles.Right)
  163.         Me.txtCity.Location = New System.Drawing.Point(16, 96)
  164.         Me.txtCity.Name = "txtCity"
  165.         Me.txtCity.Size = New System.Drawing.Size(360, 24)
  166.         Me.txtCity.TabIndex = 1
  167.         Me.txtCity.Text = ""
  168.         '
  169.         'txtZip
  170.         '
  171.         Me.txtZip.Location = New System.Drawing.Point(16, 160)
  172.         Me.txtZip.Name = "txtZip"
  173.         Me.txtZip.Size = New System.Drawing.Size(112, 24)
  174.         Me.txtZip.TabIndex = 1
  175.         Me.txtZip.Text = ""
  176.         '
  177.         'AddressControl
  178.         '
  179.         Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.Label5, Me.txtCountry, Me.Label4, Me.txtState, Me.Label2, Me.txtZip, Me.Label1, Me.txtCity, Me.txtStreet, Me.Label3})
  180.         Me.Font = New System.Drawing.Font("Microsoft Sans Serif", 11.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
  181.         Me.Name = "AddressControl"
  182.         Me.Size = New System.Drawing.Size(392, 200)
  183.         Me.ResumeLayout(False)
  184.  
  185.     End Sub
  186.  
  187. #End Region
  188.  
  189.     Dim WithEvents m_Address As New Address()
  190.  
  191.     <TypeConverter(GetType(AddressTypeConverter))> _
  192.     Property Address() As Address
  193.         Get
  194.             Return m_Address
  195.         End Get
  196.         Set(ByVal Value As Address)
  197.             m_Address = Value
  198.             RefreshControls()
  199.         End Set
  200.     End Property
  201.  
  202.     ' Refresh controls when any property changes
  203.     Private Sub Address_PropertyChanged(ByVal propertyName As String) Handles m_Address.PropertyChanged
  204.         RefreshControls()
  205.     End Sub
  206.  
  207.     ' Display Address properties in the control's fields.
  208.     Private Sub RefreshControls()
  209.         txtStreet.Text = m_Address.Street
  210.         txtCity.Text = m_Address.City
  211.         txtZip.Text = m_Address.Zip
  212.         txtState.Text = m_Address.State
  213.         txtCountry.Text = m_Address.Country
  214.     End Sub
  215.  
  216.     '---------------------------------------------
  217.     ' The TypeConverter for the Address property
  218.     '---------------------------------------------
  219.  
  220.     Public Class AddressTypeConverter
  221.         Inherits TypeConverter
  222.  
  223.         Public Overloads Overrides Function GetProperties(ByVal context As System.ComponentModel.ITypeDescriptorContext, ByVal value As Object, ByVal attributes() As System.Attribute) As System.ComponentModel.PropertyDescriptorCollection
  224.             Return TypeDescriptor.GetProperties(GetType(Address))
  225.         End Function
  226.  
  227.         Public Overloads Overrides Function GetPropertiesSupported(ByVal context As System.ComponentModel.ITypeDescriptorContext) As Boolean
  228.             Return True
  229.         End Function
  230.     End Class
  231.  
  232. End Class
  233.  
  234. '---------------------------------------------
  235. ' The class that holds Address info
  236. '---------------------------------------------
  237.  
  238. Public Class Address
  239.     Event PropertyChanged(ByVal propertyName As String)
  240.  
  241.     ' private members 
  242.     Dim m_Street As String
  243.     Dim m_City As String
  244.     Dim m_Zip As String
  245.     Dim m_State As String
  246.     Dim m_Country As String
  247.  
  248.     Property Street() As String
  249.         Get
  250.             Return m_Street
  251.         End Get
  252.         Set(ByVal Value As String)
  253.             If m_Street <> Value Then
  254.                 m_Street = Value
  255.                 RaiseEvent PropertyChanged("Street")
  256.             End If
  257.         End Set
  258.     End Property
  259.  
  260.     Property City() As String
  261.         Get
  262.             Return m_City
  263.         End Get
  264.         Set(ByVal Value As String)
  265.             If m_City <> Value Then
  266.                 m_City = Value
  267.                 RaiseEvent PropertyChanged("City")
  268.             End If
  269.         End Set
  270.     End Property
  271.  
  272.     Property Zip() As String
  273.         Get
  274.             Return m_Zip
  275.         End Get
  276.         Set(ByVal Value As String)
  277.             If m_Zip <> Value Then
  278.                 m_Zip = Value
  279.                 RaiseEvent PropertyChanged("Zip")
  280.             End If
  281.         End Set
  282.     End Property
  283.  
  284.     Property State() As String
  285.         Get
  286.             Return m_State
  287.         End Get
  288.         Set(ByVal Value As String)
  289.             If m_State <> Value Then
  290.                 m_State = Value
  291.                 RaiseEvent PropertyChanged("State")
  292.             End If
  293.         End Set
  294.     End Property
  295.  
  296.     Property Country() As String
  297.         Get
  298.             Return m_Country
  299.         End Get
  300.         Set(ByVal Value As String)
  301.             If m_Country <> Value Then
  302.                 m_Country = Value
  303.                 RaiseEvent PropertyChanged("Country")
  304.             End If
  305.         End Set
  306.     End Property
  307.  
  308.     ' override ToString 
  309.     Overrides Function ToString() As String
  310.         Return "(Address)"
  311.     End Function
  312.  
  313. End Class
  314.  
  315. ' custom license provider class
  316.  
  317. Class LicWindowsFileLicenseProvider
  318.     Inherits LicenseProvider
  319.  
  320.     Public Overrides Function GetLicense(ByVal context As LicenseContext, ByVal typ As System.Type, ByVal instance As Object, ByVal allowExceptions As Boolean) As License
  321.         ' This is the name of the control.
  322.         Dim ctrlName As String = typ.FullName
  323.  
  324.         If context.UsageMode = LicenseUsageMode.Designtime Then
  325.             ' We are at design mode: check that there is a .lic file in Windows system directory.
  326.             ' Build the full path of the .lic file.
  327.             Dim filename As String = Environment.SystemDirectory() & "\" & ctrlName & ".lic"
  328.  
  329.             Dim fs As System.IO.StreamReader
  330.  
  331.             ' open the .lic file
  332.             Try
  333.                 ' Open the license file, exception if not found.
  334.                 fs = New System.IO.StreamReader(filename)
  335.  
  336.                 ' Read its contents.
  337.                 Dim licText As String = fs.ReadToEnd
  338.                 ' Compare with the expected license text.
  339.                 If licText <> ctrlName & " is a licensed component." Then
  340.                     ' Any exception type will do.
  341.                     Throw New ArgumentException()
  342.                 End If
  343.             Catch ex As Exception
  344.  
  345.                 ' Any error throws a LicenseException object, if possible
  346.                 ' else it just returns Nothing.
  347.                 If allowExceptions Then
  348.                     Throw New LicenseException(typ, instance, "Can't find design-time license for " & ctrlName)
  349.                 Else
  350.                     Return Nothing
  351.                 End If
  352.             Finally
  353.                 ' In all cases, close the StreamReader if open.
  354.                 If Not (fs Is Nothing) Then fs.Close()
  355.             End Try
  356.  
  357.             ' If we get here, we can return a RuntimeLicense object.
  358.             Return New DesignTimeLicense(Me, typ)
  359.         Else
  360.             ' We enforce no licensing at runtime, so we always return a RunTimeLicense.
  361.             Return New RuntimeLicense(Me, typ)
  362.         End If
  363.     End Function
  364.  
  365.     ' Nested class for design-time license.
  366.  
  367.     Public Class DesignTimeLicense
  368.         Inherits License
  369.  
  370.         Private owner As LicWindowsFileLicenseProvider
  371.         Private typ As Type
  372.  
  373.         ' The constructor for this class.
  374.         Sub New(ByVal owner As LicWindowsFileLicenseProvider, ByVal typ As Type)
  375.             Me.owner = owner
  376.             Me.typ = typ
  377.         End Sub
  378.  
  379.         Overrides ReadOnly Property LicenseKey() As String
  380.             Get
  381.                 ' Just return the type name in this demo.
  382.                 Return typ.FullName
  383.             End Get
  384.         End Property
  385.  
  386.         Overrides Sub Dispose()
  387.             ' There is nothing to do here.
  388.         End Sub
  389.     End Class
  390.  
  391.     ' Nested class for run-time license.
  392.  
  393.     Public Class RuntimeLicense
  394.         Inherits License
  395.  
  396.         Private owner As LicWindowsFileLicenseProvider
  397.         Private typ As Type
  398.  
  399.         ' The constructor for this class.
  400.         Sub New(ByVal owner As LicWindowsFileLicenseProvider, ByVal typ As Type)
  401.             Me.owner = owner
  402.             Me.typ = typ
  403.         End Sub
  404.  
  405.         Overrides ReadOnly Property LicenseKey() As String
  406.             Get
  407.                 ' Just return the type name in this demo.
  408.                 Return typ.FullName
  409.             End Get
  410.         End Property
  411.  
  412.         Overrides Sub Dispose()
  413.             ' There is nothing to do here.
  414.         End Sub
  415.     End Class
  416. End Class